package project.ia04.gui.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import com.ibm.icu.util.Calendar;

public class StaffPanel extends JPanel {
	
	private static ImageIcon[] powerImage = new ImageIcon[]{
		new ImageIcon("./rsc/battery0.png"),
		new ImageIcon("./rsc/battery1.png"),
		new ImageIcon("./rsc/battery2.png"),
		new ImageIcon("./rsc/battery3.png"),
		new ImageIcon("./rsc/battery4.png"),
	};
	
	private JPanel mainPanel = new JPanel(new GridBagLayout());
	
	private JPanel descPanel = new JPanel(new FlowLayout());
	private JLabel functionLabel = new JLabel();
	private int levelLabel;
	private JLabel powerLabel = new JLabel();
	private JScrollPane scrollPane;
	private JTextArea actionTextArea = new JTextArea();
	

	public StaffPanel(String function,String name, int lvl) {
		this.setFunction(function);
		setName(name);
		setLevel(lvl);
		
		this.levelLabel = lvl;
		actionTextArea.setEditable(false);
		
		scrollPane = new JScrollPane(actionTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane.setPreferredSize(new Dimension(100,100));
		
		powerLabel.setIcon(powerImage[4]);
		
		GridBagConstraints c = new GridBagConstraints();
		
		
		descPanel.setPreferredSize(new Dimension(60,100));
		
		functionLabel.setHorizontalAlignment(JLabel.CENTER);
		descPanel.add(functionLabel);
		powerLabel.setHorizontalAlignment(JLabel.CENTER);
		descPanel.add(powerLabel);
		
		
		c.anchor = GridBagConstraints.CENTER;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 0.0;
		c.insets = new Insets(0,10,0,10);
		
		mainPanel.add(descPanel,c);
		
		c.anchor = GridBagConstraints.LINE_END;
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 1;
		c.gridy = 0;
		c.weightx = 1.0;
		c.gridheight = GridBagConstraints.REMAINDER;
		c.insets = new Insets(5, 0, 5, 10);
		
		mainPanel.add(scrollPane,c);
		
		
		setLayout(new BorderLayout());
		add(mainPanel,BorderLayout.CENTER);
		
		buildTheme();
	}

	
	public void setFunction(String function){
		functionLabel.setText(function);
	}

	
	public void setBattery(int currPower){
		if(currPower < 5)
			powerLabel.setIcon(powerImage[currPower]);
	}
	
	public void setLevel(int currLevel){
		TitledBorder titleBorder = BorderFactory.createTitledBorder("Level : " +currLevel);
		titleBorder.setTitleJustification(TitledBorder.CENTER);
		titleBorder.setTitlePosition(TitledBorder.BELOW_TOP);
		titleBorder.setTitleFont(UIManager.getFont("FontMarkerFelt").deriveFont(14f));
		TitledBorder sndtitleBorder = BorderFactory.createTitledBorder(getName());
		sndtitleBorder.setTitleFont(UIManager.getFont("FontMarkerFelt").deriveFont(Font.PLAIN,20f));
		sndtitleBorder.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		titleBorder.setBorder(sndtitleBorder);
		setBorder(titleBorder);
	}
	
	private void buildTheme(){
		//ScrollPane
		TitledBorder titleBorder = BorderFactory.createTitledBorder("Actions");
		titleBorder.setTitleFont(UIManager.getFont("FontMarkerFelt").deriveFont(16f));
		titleBorder.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		scrollPane.setBorder(titleBorder);
		scrollPane.setOpaque(false);
		
		//TextArea
		actionTextArea.setBorder(BorderFactory.createLoweredBevelBorder());
		
		//Function
		functionLabel.setFont(UIManager.getFont("FontMarkerFelt").deriveFont(13f));
		
		//PowerLabel
		powerLabel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
		
		titleBorder = BorderFactory.createTitledBorder("Level : " +levelLabel);
		titleBorder.setTitleJustification(TitledBorder.CENTER);
		titleBorder.setTitlePosition(TitledBorder.BELOW_TOP);
		titleBorder.setTitleFont(UIManager.getFont("FontMarkerFelt").deriveFont(14f));
		TitledBorder sndtitleBorder = BorderFactory.createTitledBorder(getName());
		sndtitleBorder.setTitleFont(UIManager.getFont("FontMarkerFelt").deriveFont(Font.PLAIN,20f));
		sndtitleBorder.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		titleBorder.setBorder(sndtitleBorder);
		setBorder(titleBorder);
	}
	
	public void setUnvailable(String what){
		powerLabel.setIcon(null);
		powerLabel.setBackground(Color.RED);
		powerLabel.setText(what);
	}
	
	public void setAvailable(){
		powerLabel.setText("");
		powerLabel.setBackground(this.getBackground());
	}


	public void addMessage(String newValue) {
		actionTextArea.append(newValue+"\n");
		actionTextArea.setCaretPosition(actionTextArea.getDocument().getLength());
	}
}
